package pers.wk.producer;

import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import pers.wk.redis.RedisService;
import java.util.UUID;

@Component
public class MessageSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisService redisService;

    @Qualifier(value = "producerRedisTemplate")
    @Autowired
    private RedisTemplate<String, Object> producerRedisTemplate;

    public void convertAndSend(String message, String exchange, String routingKey) {
        String msgId = UUID.randomUUID().toString();
        CorrelationData correlationData = new CorrelationData();
        correlationData.setId(msgId);
        rabbitTemplate.convertAndSend(exchange, routingKey, message, correlationData);
        //TODO 封装message对象, rabbitmq服务重启CorrelationData id可能会丢失
        rabbitTemplate.convertAndSend(exchange, routingKey, message);
        //生产发送消息，消息确认发送成功，移除消息
        redisService.setCacheObject(msgId, message);
    }
}
